package com.gjiazhe.wavesidebar.sample;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;

import java.util.ArrayList;

/**
 * Created by gjz on 9/4/16.
 */
public class ContactAdapter extends RecycleItemProvider {
    private ArrayList<Contact> mData = new ArrayList<>();
    private int mLayoutId;
    private Ability mSlice;

    public ContactAdapter(Ability abilitySlice, ArrayList<Contact> contacts, int layoutId) {
        mSlice = abilitySlice;
        mData = contacts;
        mLayoutId = layoutId;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        Component component = LayoutScatter.getInstance(mSlice).parse(mLayoutId, null, false);
        if (!(component instanceof ComponentContainer)) {
            return null;
        }
        ComponentContainer rootLayout = (ComponentContainer) component;
        Text tvIndex = (Text) rootLayout.findComponentById(ResourceTable.Id_tv_index);
        tvIndex.setText(mData.get(position).getIndex());
        Text tvName = (Text) rootLayout.findComponentById(ResourceTable.Id_tv_name);
        tvName.setText(mData.get(position).getName());
        Contact contact = mData.get(position);
        if (position == 0 || !mData.get(position-1).getIndex().equals(contact.getIndex())) {
            tvIndex.setVisibility(Component.VISIBLE);
        } else {
            tvIndex.setVisibility(Component.HIDE);
        }
        return component;
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

}
